#include "TempusImp.h"

void TempusImp ::processCommand( string comm )
{
	if ( comm.size() != 0 )
	{
		if ( comm[0] != '/')
		{
			_type = ADD;
		}
		else
		{
			switch ( comm[2])
			{
			case 's':
				_type = SHOW;
				break;
			case 'e':
				_type = EDIT;
				break;
			case 'd':
				_type = DELETE;
				break;
			default:
				_type = INVALID;
			}
		}
		if( _type != ADD)
			 removeFirstWord( comm );
		processTask(comm);
	}
}

void TempusImp ::removeFirstWord( string & word )
{
	string temp = word;
	stringstream modifier(word);
	modifier >> temp;
	word = modifier.str();
}

void TempusImp ::updateRecords()
{
	_file.saveFile();
}

void TempusImp ::processTask( string info )
{
	int i;
	istringstream info_read( info );
	ostringstream info_write;
	string tempString, lastCommand = "//details";
	vector<string> tempList;

	while ( info_read >> tempString )
	{
		tempList.push_back(tempString);
	}

	for ( i = 0; i< tempList.size(); i++ )
	{
		string currentString = tempList.at(i);
		bool  isCommand = ( currentString[0] == '/') && ( currentString[1] == '/');
		if( isCommand || (i== tempList.size()-1))
		{
			if (i== tempList.size()-1))
			{
				info_write << currentString ;
			}
			if ( lastCommand == "//location")
				_current._location = info_write.str();
			else if ( lastCommand == "//date")
				_current._date = info_write.str();
			else
				_current._details = info_write.str();
			info_write.clear();
			lastCommand = currentString;
			continue;
		}
		info_write << currentString + " ";
	}
}